package com.mz.mattress.client.api.biz;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.OrderUseTimes;
import com.mz.mattress.api.enums.DeviceOptionEnum;
import com.mz.mattress.api.jwt.ClientJwt;
import com.mz.mattress.api.mattress.req.InspectionReq;
import com.mz.mattress.client.api.req.OrderUseRecordBonusQueryReq;
import com.mz.mattress.client.api.req.OrderUseRecordQueryReq;
import com.mz.mattress.client.common.Holder;
import com.mz.mattress.client.service.OrderUseTimesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/orderUseTimes")
public class OrderUseTimesController {

    @Autowired
    private OrderUseTimesService orderUseTimesService;

    @GetMapping("/doing")
    public Result<List<OrderUseTimes>> doing() {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        Long memberId = clientJwt.getMemberId();
        //使用中
//        return orderUseTimesService.listByStatus(1, memberId);
        return orderUseTimesService.doingList(memberId);
    }

    @GetMapping("/list")
    public Result<List<OrderUseTimes>> list(@RequestParam(required = false) Long orderId) {
        return orderUseTimesService.list(orderId);
    }

    @GetMapping("/listPage")
    public Result<IPage<OrderUseTimes>> listPage(OrderUseRecordQueryReq req) {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        req.setMemberId(clientJwt.getMemberId());
        return orderUseTimesService.listPage(req);
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        return orderUseTimesService.detail(id);
    }

    @GetMapping("/queryStatus")
    public Result queryStatus(@RequestParam Integer id) {
        return orderUseTimesService.queryStatus(id);
    }

    @PostMapping("/inspection")
    public Result inspection(@RequestBody InspectionReq req) {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        Long memberId = clientJwt.getMemberId();
        req.setMemberId(memberId);
        return orderUseTimesService.inspection(req);
    }

    @GetMapping("/start")
    public Result start(@RequestParam Integer id, @RequestParam(required = false) Integer mode) {
        return orderUseTimesService.start(id, 60, mode);
    }

    @GetMapping("/stop")
    public Result stop(@RequestParam Integer id) {
        return orderUseTimesService.stop(id);
    }

    @GetMapping("/pause")
    public Result pause(@RequestParam Integer id) {
        return orderUseTimesService.pause(id);
    }

    @GetMapping("/changeOption")
    public Result changeOption(@RequestParam Integer id, @RequestParam Integer option, @RequestParam Integer val) {
        DeviceOptionEnum optionEnum = DeviceOptionEnum.valueOf(option);
        return orderUseTimesService.changeOption(id, optionEnum, val);
    }

    @GetMapping("/deviceInfo")
    public Result deviceInfo(@RequestParam Integer id) {
        return orderUseTimesService.deviceInfo(id);
    }


}
